package com.waq.service.impl;

import com.waq.mapper.CustomerMapper;
import com.waq.mapper.RegisterMapper;
import com.waq.pojo.Customer;
import com.waq.pojo.Register;
import com.waq.service.CustomerService;
import com.waq.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.waq.utils.ConvertUtils.convertCustomerToRegister;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private RegisterService registerService;
    @Override
    public List<Customer> queryCustomerList(Customer customer) {
        List<Customer> result = customerMapper.queryCustomerList(customer);
        return result;
    }

    /**
     * 咨询填写客户信息
     * @param customer
     * @return
     */
    @Override
    public Customer consultCustomer(Customer customer) {
        if (customer.getCustomerId() == null || customer.getCustomerId() == 0) {
            //在前台没登记，直接咨询
            //1.先新增登记信息
            Register register = new Register();
            register = registerService.registerCustomer(convertCustomerToRegister(customer, register));
            //2.前台登记过，未咨询 0
            customer.setHasConsulted(0);
            //3.新增customer信息
            customerMapper.insertSelective(customer);
        } else {
            //前台登记过了，已经有用户信息
            //1.前台登记过，未咨询 0
            customer.setHasConsulted(0);
            
        }

        return customer;
    }


}
